﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MITI;
using MapControl.Misc.DAL;

namespace GPSMap.Core.Misc.DAL
{
    public class DataPlaneRepository
    {
        BaseDBCe2 _dbConnect;

        public DataPlaneRepository(string dbName)
        {
            _dbConnect = new BaseDBCe2(dbName);
        }

        /// <summary>
        /// Xóa dữ liệu mẫu trong table Plane
        /// </summary>
        public void ResetData()
        {
            //===============================================================
            //***************************************************************
            //Xóa dữ liệu cũ của Plane có tên là 'heli'
            string code = "heli";
            string sql = string.Format("select * from plane where code='{0}'", code);
            if (_dbConnect.ExecSql_DataSet(sql).Tables[0].Rows.Count == 1)
                Delete(code);
            //***************************************************************
            //===============================================================

        }

        public bool IsExist(string name)
        {
            string sql = string.Format("select * from plane where name = '{0}'", name);
            if (_dbConnect.ExecSql_DataSet(sql).Tables[0].Rows.Count > 0)
                return true;
            return false;
        }

        public DataTable GetAll()
        {
            string sql = "select * from plane";
            return _dbConnect.ExecSql_DataSet(sql).Tables[0];
        }



        public void Insert(Plane objPlane)
        {
            string sql = "";
            try
            {
                sql = string.Format("insert into plane(code, name) values('{0}','{1}')", objPlane.Code, objPlane.Name);
                _dbConnect.ExecSql_NoneQuery(sql);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        public void Update(Plane objPlane)
        {
            string sql = "";
            try
            {
                sql = string.Format("update plane set code='{0}', name=N'{1}' where code='{2}'",
                                        objPlane.Code, objPlane.Name, objPlane.Code);
                _dbConnect.ExecSql_NoneQuery(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(string code)
        {
            string sql = "";
            try
            {
                sql = string.Format("delete from plane where code='{0}'", code);
                _dbConnect.ExecSql_NoneQuery(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteAll()
        {
            string sql = "";
            try
            {
                sql = "delete from plane";
                _dbConnect.ExecSql_NoneQuery(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
